package com.dx.mes.service.special.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.transcoder.util.Util;
import org.apache.poi.poifs.crypt.dsig.services.TimeStampService;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_box;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.facade.special.service.IFspboxopeService;
import com.dx.mes.facade.special.tx.Fspboxope.FspboxopeI;
import com.dx.mes.facade.special.tx.Fspboxope.FspboxopeIA;
import com.dx.mes.facade.special.tx.Fspboxope.FspboxopeO;
import com.dx.mes.facade.special.tx.Fspboxope.FspboxopeOA;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fspboxopeService")
@SuppressWarnings({"unchecked"})
public class FspboxopeService implements IFspboxopeService{
	private LogUtils logUtils;
	private static String TX_NAME = T_FSPBOXOPE;

	@Autowired
	private IRetBoxDao retBoxDao;
	
	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspboxopeService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspboxopeService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		
		Session session = retBoxDao.getCrSession();
		FspboxopeO outTrx = new FspboxopeO();
		String strOutTrx = null;
		
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		
		//输入对象
		try {
		FspboxopeI inTrx = new FspboxopeI();
		//将json字符串转换成对象
		inTrx = JacksonUtil.fromJson(strInTrx, FspboxopeI.class);
		char action_flg = inTrx.getAction_flg().charAt(0);
		
		switch (action_flg) {
			case 'C':
				cleanFnc(inTrx,outTrx);
				break;
//			case 'N':
//				cancelCleanFnc(inTrx,outTrx);
//				break;
			case 'E':
				emptyFnc(inTrx,outTrx);
				break;
			case 'T':
				noteFnc(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	
//	public void cancelCleanFnc(FspboxopeI inTrx ,FspboxopeO outTrx){
//		String evtUsr = inTrx.getEvt_usr();
//		Timestamp currentTime = Util.getCurrentTimestamp();
//		FspboxopeIA iary= inTrx.getIary().get(0);
//		String boxId = iary.getBox_id();
//		Ret_box ret_box = retBoxDao.get(boxId);
//
//		String cleanFlg = ret_box.getBox_clean_flg();
//		List<FspboxopeOA> oaryList = new ArrayList<FspboxopeOA>();
//		FspboxopeOA oary = new FspboxopeOA();
//		if(_YES.equals(cleanFlg)){
//			ret_box.setBox_clean_flg(_NO);
//			ret_box.setEvt_usr(evtUsr);
//			ret_box.setEvt_timestamp(currentTime);
//			retBoxDao.update(ret_box);
//			oary.setBox_clean_flg(ret_box.getBox_clean_flg());
//			oaryList.add(oary);
//			outTrx.setOary(oaryList);
//			outTrx.setRtn_code(RETURN_CODE_OK);
//			outTrx.setRtn_mesg("success");
//			return;
//		}else {
//			outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
//			outTrx.setRtn_mesg("箱子清洗标志为["+cleanFlg+"],不可取消清洗");
//			return;
//		}
//	}
	
	public void cleanFnc(FspboxopeI inTrx ,FspboxopeO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp currentTime = Util.getCurrentTimestamp();
		FspboxopeIA iary= inTrx.getIary().get(0);
		String boxId = iary.getBox_id();
		Ret_box ret_box = retBoxDao.get(boxId);
		if (null == ret_box) {
			String gszMsgBuf = new NotFoundRtnBeen(boxId, Ret_box.class).toString();
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		if (!ret_box.getBox_stat().trim().equals(PROC_WAIT)){
			outTrx.setRtn_mesg("卡匣["+boxId+"]状态为["+ret_box.getBox_stat().trim()+"]，必须是[WAIT]才可以做该操作。");
			outTrx.setRtn_code(String.valueOf(E_FSPBOXOPE_STATUS_INVALID));
			return;
		}
		String cleanFlg = ret_box.getBox_clean_flg();
		List<FspboxopeOA> oaryList = new ArrayList<FspboxopeOA>();
		FspboxopeOA oary = new FspboxopeOA();
		if(_YES.equals(cleanFlg)){
			ret_box.setBox_clean_flg(_NO);
			ret_box.setBox_use_cnt(0);
			ret_box.setEvt_usr(evtUsr);
			ret_box.setEvt_timestamp(currentTime);
			ret_box.setEvt_cate(EVT_CLEAN_REQUEST);
			retBoxDao.update(ret_box);

			oary.setBox_clean_flg(ret_box.getBox_clean_flg());
			oaryList.add(oary);
			outTrx.setOary(oaryList);
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			return;
		}else {
			outTrx.setRtn_code(E_FSPBOXOPE_FLAG_INVALID + "");
			outTrx.setRtn_mesg("箱子清洗标志为["+cleanFlg+"],不需要清洗");
			return;
		}
	}
	
	public void emptyFnc(FspboxopeI inTrx ,FspboxopeO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp currentTime = Util.getCurrentTimestamp();
		FspboxopeIA iary= inTrx.getIary().get(0);
		String boxId = iary.getBox_id();
		Ret_box ret_box = retBoxDao.get(boxId);
		if (null == ret_box) {
			String gszMsgBuf = new NotFoundRtnBeen(boxId, Ret_box.class).toString();
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		if (!ret_box.getBox_stat().trim().equals(PROC_EMPTY)){
			outTrx.setRtn_mesg("卡匣["+boxId+"]状态为["+ret_box.getBox_stat().trim()+"]，必须是[EMPT]才可以做该操作。");
			outTrx.setRtn_code(String.valueOf(E_FSPBOXOPE_STATUS_INVALID));
			return;
		}
		String realEmp = ret_box.getReal_emp();
		String boxStat = ret_box.getBox_stat();
		List<FspboxopeOA> oaryList = new ArrayList<FspboxopeOA>();
		FspboxopeOA oary = new FspboxopeOA();
		if(PROC_EMPTY.equals(boxStat)) {
			if(_NO.equals(realEmp)){
				ret_box.setReal_emp(_YES);
				ret_box.setEvt_usr(evtUsr);
				ret_box.setEvt_timestamp(currentTime);
				ret_box.setEvt_cate(EVT_EMPTY_REQUEST);
				retBoxDao.update(ret_box);

				oary.setReal_emp(ret_box.getReal_emp());
				oaryList.add(oary);
				outTrx.setOary(oaryList);
				outTrx.setRtn_code(RETURN_CODE_OK);
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				return;
			}else{
				outTrx.setRtn_code(E_FSPBOXOPE_FLAG_INVALID + "");
				outTrx.setRtn_mesg("卡匣不是假空状态，不需要做该操作！");
				return;
			}

		}else{
			outTrx.setRtn_code(E_FSPBOXOPE_FLAG_INVALID + "");
			outTrx.setRtn_mesg("该卡匣["+boxId+"]状态不为空，不能做该操作！");
			return;
		}
	}
	
	public void noteFnc(FspboxopeI inTrx ,FspboxopeO outTrx){
		String evtUsr = inTrx.getEvt_usr();
		Timestamp currentTime = Util.getCurrentTimestamp();
		FspboxopeIA iary= inTrx.getIary().get(0);
		String boxId = iary.getBox_id();
		String boxNote  = iary.getBox_note();
		Ret_box ret_box = retBoxDao.get(boxId);
		if (null == ret_box) {
			String gszMsgBuf = new NotFoundRtnBeen(boxId, Ret_box.class).toString();
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}

		List<FspboxopeOA> oaryList = new ArrayList<FspboxopeOA>();
		FspboxopeOA oary = new FspboxopeOA();
		if(boxNote !=null && boxNote.length()!=0){
			ret_box.setBox_note(boxNote);
			ret_box.setEvt_usr(evtUsr);
			ret_box.setEvt_cate(EVT_CST_REMARK);
			ret_box.setEvt_timestamp(currentTime);
			retBoxDao.update(ret_box);

			oary.setBox_note(ret_box.getBox_note());
			oaryList.add(oary);
			outTrx.setOary(oaryList);
			outTrx.setRtn_code(RETURN_CODE_OK);
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			return;
		}else{
			outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
			outTrx.setRtn_mesg("卡匣备注不能为空！请重新操作！");
			return;
		}
	}
	
	//错误信息
	public void errActionFnc(FspboxopeO outTrx) {
			outTrx.setRtn_code("0000009");
			outTrx.setRtn_mesg("Invalid Action_flg");
	}
}

	